var o, e = require, t = module, n = exports, i = this && this.__extends || (o = function(t, i) {
    return (o = Object.setPrototypeOf || {
        __proto__: []
    } instanceof Array && function(t, i) {
        t.__proto__ = i;
    } || function(t, i) {
        for (var e in i) i.hasOwnProperty(e) && (t[e] = i[e]);
    })(t, i);
}, function(t, i) {
    function e() {
        this.constructor = t;
    }
    o(t, i), t.prototype = null === i ? Object.create(i) : (e.prototype = i.prototype, 
    new e());
}), a = this && this.__decorate || function(t, i, e, o) {
    var s, n = arguments.length, h = n < 3 ? i : null === o ? o = Object.getOwnPropertyDescriptor(i, e) : o;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) h = Reflect.decorate(t, i, e, o); else for (var r = t.length - 1; 0 <= r; r--) (s = t[r]) && (h = (n < 3 ? s(h) : 3 < n ? s(i, e, h) : s(i, e)) || h);
    return 3 < n && h && Object.defineProperty(i, e, h), h;
};

Object.defineProperty(n, "__esModule", {
    value: !0
});

var r = cc._decorator, s = r.ccclass, c = r.property, l = function(e) {
    function t() {
        var t = null !== e && e.apply(this, arguments) || this;
        return t.scrollView = null, t.view = null, t.itemPrefab = null, t.itemType = "SkinItemView", 
        t.spacingY = 0, t.data = [], t.rowItemCounts = 0, t.items = [], t.topMax = 0, t.bottomMax = 0, 
        t.lastListY = 0, t.itemHeight = 0, t.isUpdate = !1, t;
    }
    return i(t, e), t.prototype.refreshPanel = function() {
        this.data = [], this.rowItemCounts = 0, this.items = [], this.topMax = 0, this.bottomMax = 0, 
        this.lastListY = 0, this.itemHeight = 0;
    }, t.prototype.init = function(t, i) {
        if (void 0 === i && (i = 0), this.scrollView && (this.refreshPanel(), t && !(t.length <= 0))) {
            this.data = t.slice(), console.log("this.data:", this.data);
            var e = cc.instantiate(this.itemPrefab);
            this.itemHeight = e.height, this.rowItemCounts = Math.ceil(this.view.height / (this.itemHeight + this.spacingY));
            for (var o = 0; o < this.rowItemCounts + 8 && (console.log("this.data[i]:", this.data[o]), 
            void 0 !== this.data[o]); ++o) {
                var s = e.getComponent(this.itemType);
                s.init(this.data[o]), this.items.push(s), this.items[o].__itemID = o, this.scrollView.content.addChild(s.node), 
                s.node.x = 0, s.node.y = -(this.itemHeight / 2 + o * (this.itemHeight + this.spacingY)), 
                o < this.rowItemCounts + 9 && (e = cc.instantiate(this.itemPrefab));
            }
            this.scrollView.content.height = 20 + this.data.length * (this.itemHeight + this.spacingY), 
            this.topMax = 5 * this.itemHeight + 4 * this.spacingY, this.bottomMax = -(this.view.height + this.topMax), 
            this.lastListY = this.scrollView.content.y, this.scrollView.content.y = i * (this.itemHeight + this.spacingY), 
            this.isUpdate = !0;
            i = new cc.Component.EventHandler();
            i.target = this.node, i.component = "MyScrollView", i.handler = "scrollowCallback", 
            this.scrollView.scrollEvents.push(i);
        }
    }, t.prototype.scrollowCallback = function() {
        if (this.scrollView && this.isUpdate) {
            for (var t = this.scrollView.content.y > this.lastListY, i = this.items.length, e = this.data.length, o = 0; o < i; o++) {
                var s = this.items[o];
                if (!s || !s.node) break;
                var n, h = cc.v2(this.scrollView.content.convertToWorldSpaceAR(s.node.getPosition()));
                if (h.y -= this.view.height / 2, h = cc.v2(this.view.convertToNodeSpaceAR(h)), t) {
                    if (h.y > this.topMax) {
                        if ((n = s.__itemID + i) >= e) return;
                        s.__itemID = n, s.node.y = s.node.y - i * (this.itemHeight + this.spacingY), s.init(this.data[s.__itemID]);
                    }
                } else if (h.y < this.bottomMax) {
                    if ((n = s.__itemID - i) < 0) return;
                    s.__itemID = n, s.node.y = s.node.y + i * (this.itemHeight + this.spacingY), s.init(this.data[s.__itemID]);
                }
            }
            this.lastListY = this.scrollView.content.y;
        }
    }, a([ c(cc.ScrollView) ], t.prototype, "scrollView", void 0), a([ c(cc.Node) ], t.prototype, "view", void 0), 
    a([ c(cc.Prefab) ], t.prototype, "itemPrefab", void 0), a([ c ], t.prototype, "itemType", void 0), 
    a([ c ], t.prototype, "spacingY", void 0), a([ s ], t);
}(cc.Component);

n.default = l;